import 'dart:ui';

import 'package:common/base/base_stateless_widget.dart';
import 'package:common/constant/constant.dart';
import 'package:common/utils/channel_util.dart';
import 'package:common/utils/sp_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:home/mvp/view/main_page.dart';
import 'package:main_fl/route_config.dart';
import 'package:resource/generated/l10n.dart';
import 'package:user/mvp/view/login_page.dart';

///判断是否登录
bool? isLogin = false;

///flutter 程序默认主入口，
///对应android DartEntrypoint createDefault()
@pragma('vm:entry-point')
void main() async {
  initFlutterEnter();
  ChannelUtil.initChannel();
}

///flutter 缓存程序默认主入口，
///对应android
///DartExecutor.DartEntrypoint(flutterLoader.findAppBundlePath(),"cacheMain")
@pragma('vm:entry-point')
void cacheMain() {
  initFlutterEnter();
}

void initFlutterEnter(){
  //初始化Widget
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();
  //设置坚屏显示
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  //显示状态栏，导航栏
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
      overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
  SpUtil.getString(Constant.tokenKey).then((value) {
    isLogin = value.isBlank == true ? false : true;
   runApp(const MyApp());
  });
}



class MyApp extends BaseStatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget onCreate(BuildContext context) {
    return MaterialApp(
      routes: RouteConfig().initRouteConfig(context),
      theme: ThemeData(
        useMaterial3: true,
        appBarTheme: const AppBarTheme(
          titleTextStyle: TextStyle(fontSize: 16,fontWeight: FontWeight.w600),
          backgroundColor: Colors.teal,
          foregroundColor: Colors.white,
          toolbarHeight: 44,
        )
      ),
      localizationsDelegates: const [
        S.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,
      home: ((isLogin == null) || (isLogin == false)) ?  const LoginPage() : const MainPage()
    );
  }
}
